package com.wgj.android.web;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import com.alibaba.fastjson.JSONObject;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.utils.http.entity.VUpload;
import com.jiaai.xui.view.XTakePhotosDialog;
import com.jiaai.xui.view.widget.XLoadingDialog;
import com.jiaai.xui.view.widget.XToast;
import com.just.agentweb.AgentWeb;
import com.wgj.android.R;
import com.wgj.android.dialog.CardShareDialog;
import com.wgj.android.ui.activity.CreateCardActivity;
import com.wgj.android.ui.activity.VIPInterfaceActivity;
import com.wgj.android.ui.activity.VideoTuiGuangActivity;
import com.wgj.android.widget.imageselect.ImagePicker;
import com.wgj.android.widget.imageselect.cropper.CropImage;
import com.wgj.android.widget.imageselect.cropper.CropImageView;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FragmentInterface {
    private Handler deliver = new Handler(Looper.getMainLooper());
    private AgentWeb agent;
    private Fragment context;
    private ImagePicker imagePicker;
    public FragmentInterface(AgentWeb agent, android.support.v4.app.Fragment context) {
        this.agent = agent;
        this.context = context;
        imagePicker = new ImagePicker();
        // 设置标题
        imagePicker.setTitle("选择图片");
        // 设置是否裁剪图片
        imagePicker.setCropImage(true);
    }
    public  void onActivityResult(int requestCode, int resultCode, Intent data){
        imagePicker.onActivityResult(context, requestCode, resultCode, data);
    }
    @JavascriptInterface
    public void selectImages() {
        new XTakePhotosDialog(context.getContext(), R.style.dialog_setting).setTakePhotosListener(new XTakePhotosDialog.OnTakePhotosListener() {
            @Override
            public void onTakePictureClick() {
                imagePicker.startCamera(context, new ImagePicker.Callback() {
                    @Override
                    public void onPickImage(Uri imageUri) {
                    }
                    // 自定义裁剪配置
                    @Override public void cropConfig(CropImage.ActivityBuilder builder) {
                        builder
                                // 是否启动多点触摸
                                .setMultiTouchEnabled(false)
                                // 设置网格显示模式
                                .setGuidelines(CropImageView.Guidelines.OFF)
                                // 圆形/矩形
                                .setCropShape(CropImageView.CropShape.RECTANGLE)
                                // 调整裁剪后的图片最终大小
                                .setRequestedSize(360, 360)
                                // 宽高比
                                .setAspectRatio(1, 1);
                    }
                    @Override
                    public void onCropImage(Uri imageUri) {
                        uploadAvater(imageUri.getPath());

                    }
                });
            }

            @Override
            public void onChoosePictureClick() {
                imagePicker.startGallery(context, new ImagePicker.Callback() {
                    @Override
                    public void onPickImage(Uri imageUri) {
                    }
                    // 自定义裁剪配置
                    @Override public void cropConfig(CropImage.ActivityBuilder builder) {
                        builder
                                // 是否启动多点触摸
                                .setMultiTouchEnabled(false)
                                // 设置网格显示模式
                                .setGuidelines(CropImageView.Guidelines.OFF)
                                // 圆形/矩形
                                .setCropShape(CropImageView.CropShape.RECTANGLE)
                                // 调整裁剪后的图片最终大小
                                .setRequestedSize(360, 360)
                                // 宽高比
                                .setAspectRatio(1, 1);
                    }
                    @Override
                    public void onCropImage(Uri imageUri) {
                        uploadAvater(imageUri.getPath());
                    }
                });

            }
        }).show();

    }
    @JavascriptInterface
    public void showLoading(String messge) {
        if(TextUtils.isEmpty(messge)){
            XLoadingDialog.with(context.getContext()).setMessage(messge).show();
        }else {
            XLoadingDialog.with(context.getContext()).show();
        }

    }
    @JavascriptInterface
    public void hidLoadIng() {
        XLoadingDialog.with(context.getContext()).hide();

    }
    @JavascriptInterface
    public void editCard(String cards) {
        Intent intent=new Intent(context.getContext(), CreateCardActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("cards",cards);
        intent.putExtras(bundle);
        context.startActivityForResult(intent,1000);
    }
    @JavascriptInterface
    public void shareCard(String cards) {
         CardShareDialog.getInstael(cards).show(context.getActivity().getSupportFragmentManager(),"CardShareDialog");
    }
    @JavascriptInterface
    public void selectMap() {

    }
    @JavascriptInterface
    public void VIPInterface(String merchant) {
        context.startActivity(new Intent(context.getContext(), VIPInterfaceActivity.class).putExtra("merchant",merchant));
    }


    @JavascriptInterface
    public  void toast(String message){
        if(message.indexOf("{")==0){
            JSONObject object=JSONObject.parseObject(message);
             switch (object.getString("type")){
                 case "error":
                     XToast.error(object.getString("msg"));
                     return;
                 case "success":
                     XToast.success(object.getString("success"));
                     return;
             }
        }else {
            XToast.success(message);
        }

    }
    @JavascriptInterface
    public void toOwn(String str) {
        context.startActivity(new Intent(context.getContext(), VideoTuiGuangActivity.class).putExtra("video",str));
        //context.startActivity(new Intent(context, PlayerActivity.class).putExtra("video",str));
    }

    @JavascriptInterface
    public void exit() {
        ((Activity)context.getContext()).finish();
    }
    private  void uploadAvater(String path){
        XLoadingDialog.with(context.getContext()).show();
        Map<String,File> params=new HashMap<>();
        params.put("file",new File(path));
        XHttp.obtain().upload("wgj", params, new ListHttpCallBack<VUpload>() {
            @Override
            public void onSuccess(final List<VUpload> list) {
                XLoadingDialog.with(context.getContext()).dismiss();
                String path =list.get(0).getFilePath();
                agent.getJsAccessEntrace().quickCallJs("window.onSelectImage('"+path+"')");
                XToast.success("修改成功");
            }
            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("上传图片失败");
                XLoadingDialog.with(context.getContext()).dismiss();
            }
        });
    }


}
